package edu.westga.sketchit2009cs.stringBufferServer;

import java.net.ServerSocket;
import java.net.Socket;

public class DrawingServer implements Runnable{

	private ServerSocket server;
	private Socket client;
	public static SynchronizedBuffer theBuffer;
	
	public DrawingServer() {
		try {
			server = new ServerSocket(4999);
			System.out.println("Hosting on 4999");
			theBuffer = new SynchronizedBuffer();
		} catch (Exception e) {
			System.out.println("Could not listen on port 4999");
		}
	}
	
	public void run() {
		while(true) {
			
			ClientWorker w;
			
			try {
				client = server.accept();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			
			try {
				//server.accept returns a client connection
				w = new ClientWorker(client);
				Thread t = new Thread(w);
				t.start();
			} catch (Exception e) {
				System.out.println("Accept failed: 4999");
			}  
		}
	}
	
}
